﻿using System.ComponentModel;
using System.IO;
using Microsoft.Build.Evaluation;
using System.Windows.Controls;

namespace FxArchitect.ContentPipeline
{
    public class ContentFile
    {
        const string CAT_XNA_FRAMEWORK = "XNA Framework Content Pipeline";

        string m_assetName, m_name, m_fullpath;
        ContentImporterInfo m_importer;
        ContentProcessorInfo m_processor;
        TreeViewItem m_root;

        [Category(CAT_XNA_FRAMEWORK)]
        [DisplayName("File Name")]
        [Description("Name of the source file.")]
        [ReadOnly(true)]
        public string Name { get { return m_name; } }

        [Category(CAT_XNA_FRAMEWORK)]
        [DisplayName("Full Path")]
        [Description("Full path to the source file.")]
        [ReadOnly(true)]
        public string FullPath
        {
            get { return m_fullpath; }
            set
            {
                m_fullpath = value;
                FileInfo fileInfo = new FileInfo(m_fullpath);
                m_name = new FileInfo(m_fullpath).Name;
                m_assetName = fileInfo.Name.Replace(fileInfo.Extension, "");
            }
        }

        [Category(CAT_XNA_FRAMEWORK)]
        [DisplayName("Asset Name")]
        [Description("The name that will be used to refer to this content item.")]
        public string AssetName { get { return m_assetName; } set { m_assetName = value; } }

        [Category(CAT_XNA_FRAMEWORK)]
        [DisplayName("Content Importer")]
        [Description("The importer used to read the content file.")]
        [TypeConverter(typeof(ContentImporterConverter))]
        public ContentImporterInfo Importer
        {
            get { return m_importer; }
            set { m_importer = value.Clone(); }
        }

        [Category(CAT_XNA_FRAMEWORK)]
        [DisplayName("Content Processor")]
        [Description("The processor used to produce the game data.")]
        [TypeConverter(typeof(ContentProcessorConverter))]
        public ContentProcessorInfo Processor
        {
            get { return m_processor; }
            set { m_processor = value.Clone(); }
        }

        [Browsable(false)]
        public TreeViewItem TreeNodes { get { return m_root; } }

        public ContentFile(string fullpath)
        {
            m_fullpath = fullpath;
            FileInfo fileInfo = new FileInfo(m_fullpath);
            m_name = fileInfo.Name;
            m_assetName = fileInfo.Name.Replace(fileInfo.Extension, string.Empty);

            m_root = new TreeViewItem();
            m_root.Header = m_name;
            m_root.Tag = this;
            //m_root = new TreeNode(m_name);
            //m_root.Tag = this;
            //m_root.ContextMenuStrip = MainForm.ContentItem_MenuStrip;
        }

        /// <summary>
        /// Writes this item to the content project.
        /// </summary>
        /// <param name="project"></param>
        public ProjectItem WriteItem(Project project)
        {
            ProjectItem item = project.AddItem("Compile", m_fullpath)[0];
            item.SetMetadataValue("Link", Path.GetFileName(m_fullpath));
            item.SetMetadataValue("Name", m_assetName);
            if (m_importer != null)
                item.SetMetadataValue("Importer", m_importer.TypeName);
            if (m_processor != null)
                item.SetMetadataValue("Processor", m_processor.TypeName);
            return item;
        }

        public override string ToString() { return m_name; }
    }
}
